<?php

namespace app\admin\controller;
use think\Db;
use think\Request;

class Room extends Common
{

    // 房间分类管理
    public function cate()
    {
        if (request()->isPost()) {
            $data = input('post.');

            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('common.page_sizes');
            $list = db('room_cate')
                ->order('id desc')
                ->paginate(array('list_rows' => $pageSize, 'page' => $page))
                ->toArray();
            $list = model('roomCate')->listAddTimeInfo($list);
            return $result = ['code' => 0, 'msg' => config('common.data_success'), 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }
        return $this->fetch('');
    }
    // 更变房间状态
    public function cateStatus()
    {
        if (request()->isPost()) {
            $data = input('post.');
            return model('roomCate')->commonState($data, '状态更变成功', '状态更变失败，请稍后重试');

        }
    }
    // 房间分类删除
    public function cateDel()
    {
        if (request()->isPost()) {
            $data = input('post.')['id'];
            return model('roomCate')->commonDel($data, '删除成功', '删除失败，请重试');
        }
    }
    // 房间分类编辑
    public function catEdit()
    {
        if (request()->isPost()) {
            $data = input('post.');
            return model('roomCate')->commonData($data, '修改成功!', '修改失败，请重试!');
        } else {
            $id = input('id');
            $list = db('roomCate')->where(['id' => $id])->find();
            return $this->fetch('', ['list' => $list]);
        }
    }
    // 房间分类添加
    public function cateAdd()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $data['addtime'] = time();
            return model('RoomCate')->commonData($data, '添加成功', '添加失败，请重试');
        }
        return $this->fetch('');

    }

    // 房间管理   列表页
    public function index()
    {
        if (request()->isPost()) {
            $data = input('post.');

            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('common.page_sizes');
            $list = db('room')
                ->alias('r')
                ->join('room_cate c', 'r.two_cate = c.id')
                ->order('r.id desc')
                ->field('r.id as id,r.logo as logo,r.one_cate as one_cate,r.is_activity as is_activity,c.title as two_cate,r.status as status,r.new_price as new_price,r.old_price as old_price')
                ->paginate(array('list_rows' => $pageSize, 'page' => $page))
                ->toArray();
            return $result = ['code' => 0, 'msg' => config('common.data_success'), 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }
        return $this->fetch('');
    }
    // 更变房间状态
    public function status()
    {
        if (request()->isPost()) {
            $data = input('post.');
            return model('room')->commonState($data, '状态更变成功', '状态更变失败，请稍后重试');
        }
    }
    // 是否活动房
    public function activity()
    {
        if (request()->isPost()) {
            $data = input('post.');
            return model('room')->commonState($data, '状态更变成功', '状态更变失败，请稍后重试');
        }
    }
    // 更变房间状态
    public function indexAutomatic()
    {
        if (request()->isPost()) {
            $data = input('post.');
            return model('room')->commonState($data, '状态更变成功', '状态更变失败，请稍后重试');
        }
    }
// 房间编辑
    public function Edit()
    {
        if (request()->isPost()) {
            $data = input('post.');
            return model('room')->commonData($data, '修改成功!', '修改失败，请重试!');
        } else {
            $id = input('id');
            $list = db('room')->where(['id' => $id])->find();
            $list_category = db('room_cate')->where(['status' => 0])->order('id asc')->select();
            return $this->fetch('', ['list' => $list, 'list_category' => $list_category]);
        }
    }
    // 房间分类添加
    public function Add()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $data = unmArr($data);
            $room_id = db('room')->insertGetId($data);
            $arr = db('time')->field('id')->select();
            $arr_count = count($arr);
            $datas = array();
            for($i = 0;$i < $arr_count;$i++){
                $datas[] = [
                    'time_id' => $arr[$i]['id'],
                    'room_id' => $room_id,
                    'price' => $data['new_price'],
                    'num' => $data['room_num']
                ];
            }
            db('room_price')->insertAll($datas);
            return [
                'code'=>'200',
                'msg'=>'添加数据',
                'errorCode'=>'8888',
                'url' => url('index')
            ];
        } else {
            return $this->fetch('');
        }
    }
    // 房间删除
    public function del()
    {
        if (request()->isPost()) {
            $data = input('post.')['id'];
            return model('room')->commonDel($data, '删除成功', '删除失败，请重试');
        }
    }

    // 获取分类
    public function getcate()
    {
        if (request()->isPost()) {
            $data = input('post.');
            // 房间的状态
            $data['status'] = 0;
            $list = db('room_cate')->where($data)->select();
            return $result = ['code' => 0, 'msg' => config('common.data_success'), 'data' => $list, 'rel' => 1];

        }
    }
    // 订单管理
    public function order()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('common.page_sizes');
            $phone = input('post.phone');
            $status = input('post.status');
            $paytime = input('post.paytime');
            $oldtime = input('post.oldtime');
            $order_number = input('post.order_number');
            $where = array();
            if(isset($phone)){
                $where['u.phone']=$phone;
            }
            if(isset($status)){
                $where['o.status'] = $status;
            }
            if(isset($order_number)){
                $where['o.order_number'] = $order_number;
            }
            if(isset($paytime) && isset($oldtime)){
                $where['o.paytime'] = array('between',$paytime.','.$oldtime);
            }else{
                
                if(isset($oldtime)){
                    $where['o.paytime']=array('elt',$oldtime);
                }
                if(isset($paytime)){
                    $where['o.paytime']=array('egt',$paytime);
                }
                if(!isset($paytime) && !isset($oldtime)){
                    $date = date('Y/m');
                    $day = date('d');
                    $paytime = $date.'/'.$day;
                    $oldtime = $date.'/'.($day*1+1);
                    $paytime = strtotime($paytime);
                    $oldtime = strtotime($oldtime);
                    $where['o.paytime'] = array('between',$paytime.','.$oldtime);
                }
            }
            
            $list = db('order')
                ->alias('o')
                ->join('user u', 'o.user_id = u.user_id')
                ->join('room r', 'o.room_id =r.id')
                ->join('room_cate c', 'r.two_cate = c.id')
                ->where($where)
                ->order('o.order_id desc')
                ->field('u.realname as realname,u.phone as phone,o.order_id as id,r.one_cate as one_cate,c.title as two_cate,o.order_number as order_number,u.realname as realname,u.phone as phone,o.days as days,o.money as money,o.addtime as addtime,o.status as status,o.type as type')
                ->paginate(array('list_rows' => $pageSize, 'page' => $page))
                ->toArray();
            $list = model('Order')->listAddTimeInfo($list);
            return $result = ['code' => 0, 'msg' => config('common.data_success'), 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }
        return $this->fetch('');
    }
    // 订单查询
    public function orderEdit()
    {
        if (request()->isPost()) {
            $data = input('post.');
            
            $data['userlist'] = json_decode($data['userlist']);
            
            $data['userlist'] = serialize($data['userlist']);
            if($data['status']==5){
                $user = db('order')
                    ->alias('o')
                    ->join('user u','u.user_id = o.user_id')
                    ->where(['order_id'=>$data['order_id']])
                    ->field('ordermoney,order_number,father_id,type')
                    ->find();
                if($user['type']==0&&$user['father_id']!=0){
                    $father = db('user')->where(['user_id'=>$user['father_id']])->find();
                    $info = db('hotel')->find();
                    $num = $father['money']*1+round($user['ordermoney']*$info['room_com']/100,2);
                    $father['money'] = $num;
                    db('user')->where(['user_id'=>$father['user_id']])->update(['money'=>$father['money']]);
                    $sql = db('user_money_log')->insert(['user_id'=>$father['user_id'],'money'=>round($user['ordermoney']*$info['room_com']/100,2),'type'=>0,'order_num'=>$user['order_number'],'addtime'=>time(),'desc'=>'推荐人订房返余额']);
                }
            }
            return model('order')->orderData($data, '修改成功!', '修改失败，请重试!');
        } else {
            $id = input('id');
            $list = db('order')
                ->alias('o')
                ->join('user u','o.user_id = u.user_id')
                ->join('room r','r.id = o.room_id')
                ->join('room_cate c','r.two_cate = c.id ')
                ->join('invoice i','o.invoice_id = i.id','LEFT')
                ->where(['o.order_id' => $id])
                ->field(
                    'o.order_id as order_id,o.order_number as order_number,r.one_cate as one_cate,c.title as two_cate,
                    r.is_activity as is_activity,o.days as days,o.addtime as addtime,o.paytime as paytime,o.type as type,
                    o.money as money,o.in_time as in_time,o.out_time as out_time,o.invoice_id as invoice_id,
                    i.cate_id as invoice_cateid,i.name as name,i.company as company,i.indentify_num as indentify_num,
                    deposit,invoice_status,
                    i.address as address,i.phone as phone,i.bank as bank,i.number as number,o.idcard as idcard,o.status as status,o.userlist as userlist,o.refund_type as refund_type,o.refund_note as refund_note')
                ->order('o.order_id desc')
                ->find();
                
                $userlist = unserialize($list['userlist']);
                foreach ($userlist as $i => $item){
                   if(is_object($item)){
                        $userlist[$i] = (array)$item;
                   }
                }
                foreach ($userlist as $i => $item) {
                    if(!isset($item['name'])){
                        $userlist[$i]['name'] = '';
                    }
                    if(!isset($item['phone'])){
                        $userlist[$i]['phone'] = '';
                    }
                    if(!isset($item['sex'])){
                        $userlist[$i]['sex'] = '3';
                    }
                    if(!isset($item['idcard'])){
                        $userlist[$i]['idcard'] = '';
                    }
                    if(!isset($item['address'])){
                        $userlist[$i]['address'] = '';
                    }
                }
            $list['userlist'] = $userlist;
            return $this->fetch('', ['list' => $list]);
        }
    }
    public function price(){
        if (request()->isPost()) {
            $post = input('post.');
            $date = date('Y-m-d');
            $time = time();
            $week = date('w',$time);
            if($week != 0){
                $timer = 3600*24*$week;
                $timer = $time-$timer;
                $date = date('Y-m-d',$timer);
            }
            $list = db('room_price')
                ->alias('p')
                ->join('time t','t.id = p.time_id')
                ->where(['p.room_id'=>$post['id'],'t.date'=>['EGT',$date]])
                ->order('t.date asc')
                ->field('p.id,t.date,p.price,p.room_id,p.num,p.status,p.use_num')
                ->page($post['page'].',7')
                ->select();
            $pages = db('room_price')
                ->alias('p')
                ->join('time t','t.id = p.time_id')
                ->where(['p.room_id'=>$post['id'],'t.date'=>['EGT',$date]])
                ->count();
            $pages = ceil($pages/7);
            $res = ['data'=>$list,'pages'=>$pages];
            return $res;
        } else {
            $where = array();
            $list = db('room')
                ->alias('r')
                ->join('room_cate c','r.two_cate = c.id')
                ->order('r.one_cate asc,r.is_activity asc')
                ->field('c.title,r.is_activity,r.one_cate,r.id')
                ->select();
            $this->assign('list',$list);
            return $this->fetch('');
        }
    }
    public function priceupdate(){
        if(request()->isPost()){
            $post = input('post.');
            $res = db('room_price')->where(['id'=>$post['id']])->update(['num'=>$post['num'],'price'=>$post['price'],'status'=>$post['status']]);
            if($res){
                return ['code'=>200,'msg'=>'更新成功'];
            }else{
                return ['code'=>0,'msg'=>'更新失败'];
            }
        }
    }
    public function priceedit(){
        if(request()->isPost()){
            $post = input('post.');
            $res = db('time')->where(['date'=>['BETWEEN',[$post['start_time'],$post['end_time']]]])->select();
            $arr_id = array_column($res, 'id');
            $res = db('room_price')->where(['room_id'=>$post['id'],'time_id'=>['IN',$arr_id]])->update(['status'=>$post['status'],'price'=>$post['price'],'num'=>$post['num']]);
            if($res){
                return ['code'=>200,'msg'=>'更新成功'];
            }else{
                return ['code'=>0,'msg'=>'更新失败'];
            }
        }else{
            $id = input('id');
            $list = db('room')->where(['id'=>$id])->find();
            $list['two_cate'] = db('room_cate')->where(['id'=>$list['two_cate']])->value('title');
            $this->assign('list',$list);
            return $this->fetch('');
        }
    }
}
